package com.lujianfei.sugarorm;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView tv_info;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_info = findViewById(R.id.tv_info);
        findViewById(R.id.btn_add).setOnClickListener(this);
        findViewById(R.id.btn_del).setOnClickListener(this);
        findViewById(R.id.btn_modify).setOnClickListener(this);
        findViewById(R.id.btn_query).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_add:
            {
                Entity saveEntity = new Entity();
                saveEntity.setName("Peter");
                saveEntity.setUpdateTime(System.currentTimeMillis());
                long id = saveEntity.save();
                tv_info.append("-----------------------------\r\n");
                tv_info.append(String.format("add %s id = %s\r\n",saveEntity.toString(),id));
            }
                break;
            case R.id.btn_del:
            {
                int affectedRows;
                affectedRows = Entity.deleteAll(Entity.class);
//                affectedRows = Entity.deleteAll(Entity.class,"name like ?","Peter");
                tv_info.append("----------------------------------------------------------\r\n");
                tv_info.append(String.format("deleteAll %s rows\r\n",affectedRows));
            }
                break;
            case R.id.btn_modify:
            {
                List<Entity> entities = Entity.listAll(Entity.class);
                if (null != entities && !entities.isEmpty()) {
                    for (Entity entity :
                            entities) {
                        entity.setName("John");
                        entity.setUpdateTime(System.currentTimeMillis());
                        entity.save();
                    }
                    tv_info.append("----------------------------------------------------------\r\n");
                    tv_info.append(String.format("modify %s rows\r\n",entities.size()));
                }
            }
                break;
            case R.id.btn_query:
            {
                List<Entity> entities = Entity.listAll(Entity.class);
                tv_info.append("----------------------------------------------------------\r\n");
                tv_info.append(String.format("queryAll %s\r\n",entities));
            }
                break;
        }
    }
}
